﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace DAO
{
    public class KhuyenMaiDAO
    {
        public List<KhuyenMaiDTO> LayDanhSachKhuyenMai()
        {
            List<KhuyenMaiDTO> listKM = new List<KhuyenMaiDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from KhuyenMai";

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    KhuyenMaiDTO kmDTO = new KhuyenMaiDTO();
                    kmDTO.ID_KhuyenMai = reader.GetInt32(0);
                    kmDTO.TenKhuyenMai = reader.GetString(1);
                    kmDTO.NgayBatDau = reader.GetDateTime(2);
                    kmDTO.NgayKetThuc = reader.GetDateTime(3);
                    kmDTO.ChietKhau = reader.GetInt32(4);
                    kmDTO.TinhTrang = reader.GetInt32(5);

                    listKM.Add(kmDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listKM;
        }

        public List<KhuyenMaiDTO> LayChiecKhau_theoma(int makhuyenmai)
        {
            List<KhuyenMaiDTO> listKM = new List<KhuyenMaiDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select * from KhuyenMai where ID_KhuyenMai="+makhuyenmai+"";

                conn = DataProvider2.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    KhuyenMaiDTO kmDTO = new KhuyenMaiDTO();
                    kmDTO.ID_KhuyenMai = reader.GetInt32(0);
                    kmDTO.TenKhuyenMai = reader.GetString(1);
                    kmDTO.NgayBatDau = reader.GetDateTime(2);
                    kmDTO.NgayKetThuc = reader.GetDateTime(3);
                    kmDTO.ChietKhau = reader.GetInt32(4);
                    kmDTO.TinhTrang = reader.GetInt32(5);

                    listKM.Add(kmDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listKM;
        }
    }
}
